package com.kaci.springbootormlite;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

    public class ScheduledThreadPoolExample {
        public static void main(String[] args) throws InterruptedException {
            // 创建 ScheduledThreadPool 线程池
            int MIN_IO_THREAD_POOL_SIZE = Math.max(64, Runtime.getRuntime().availableProcessors());
            System.out.println("MIN_IO_THREAD_POOL_SIZE：" + MIN_IO_THREAD_POOL_SIZE);
            ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);
            System.out.println("scheduleWithFixedDelay 方法添加任务：" + LocalDateTime.now());
            threadPool.scheduleWithFixedDelay(new Runnable() {
                                                  @Override
                                                  public void run() {
                                                      System.out.println("执行 scheduleWithFixedDelay 方法：" + LocalDateTime.now());
                                                      // 休眠 5s
                                                      try {
                                                          TimeUnit.SECONDS.sleep(20);
                                                      } catch (InterruptedException e) {
                                                          e.printStackTrace();
                                                      }
                                                  }
                                              },
                    3L, // 3s 后开始执行定时任务
                    2L, // 定时任务执行完 2s 之后，再执行下一个定时任务
                    TimeUnit.SECONDS); // 描述上面两个参数的时间单位
        //scheduleAtFixedRate
            threadPool.scheduleAtFixedRate(new Runnable() {
                                                  @Override
                                                  public void run() {
                                                      System.out.println("执行 scheduleWithFixedDelay 5s方法：" + LocalDateTime.now());
                                                      // 休眠 10s
                                                      try {
                                                          TimeUnit.SECONDS.sleep(6);
                                                      } catch (InterruptedException e) {
                                                          e.printStackTrace();
                                                      }
                                                  }
                                              },
                    3L, // 3s 后开始执行定时任务
                    5L, // 定时任务执行完 5s 之后，再执行下一个定时任务
                    TimeUnit.SECONDS); // 描述上面两个参数的时间单位
        }
    }

